<template>
  <div class="work">

    <div class="title">
      Reimbursement List
    </div>

    <!-- 搜索框 -->
    <div class="search">
          <div>
            <el-button type="primary" @click="add"><span style="padding-left:3px"><i class="el-icon-circle-plus-outline"></i>Add</span></el-button>
          </div>
          <div style="width:30%">
            <el-input v-model="searchName" placeholder="please enter">
              <el-button @click="search" slot="append">search</el-button>
              <el-button @click="clearSearch" slot="append">cancel search</el-button>
            </el-input>
          </div>
    </div>

    <!-- 主体内容 -->
    <template>
      <el-table
          :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
          :data="tableData"
          border
          style="width: 100%"
          ref="tableData"
          class="emp_table"
          v-if="tableData">
          <el-table-column label="S/N" type="index" width="60" align="center"></el-table-column>
          <el-table-column
          align="center"
          prop="expense_id"
          label="Expense Account ID"
          width="180">
          </el-table-column>
          <el-table-column
          align="center"
          prop="expense_name"
          label="Name">
          </el-table-column>
          <el-table-column
          align="center"
          prop="expense_project_name"
          label="Project">
          </el-table-column>
          <el-table-column
          align="center"
          prop="total_amount"
          label="Amount">
          </el-table-column>
          <el-table-column
          align="center"
          prop="expense_payment_currency"
          label="Payment Currency">
          </el-table-column>
          <el-table-column
          align="center"
          prop="expense_type"
          label="Type">
          </el-table-column>
          <el-table-column
          align="center"
          prop="expense_submit_time"
          label="Submit Time">
          </el-table-column>
          <el-table-column
          align="center"
          prop="status_name"
          label="Status">
          <template slot-scope="{ row }">
            {{ row.status_name }}
          </template>
          </el-table-column>
        <el-table-column
          align="center"
          label="Action">
          <template slot-scope="scope">
              <el-button type="text" size="small" @click="see(scope)">see details</el-button>
              <el-button type="text" size="small" @click="deleteData(scope.row)" :disabled="scope.row.status > 0" v-if="key != 1">delete</el-button>
              <el-button type="text" size="small" @click="edit(scope.row)" :disabled="scope.row.status > 0 " v-if="key != 1">edit</el-button>
          </template>
          </el-table-column>
      </el-table>
    </template>

    <!--分页-->
    <el-pagination
      align="right"
      @current-change="curChange"
      :current-page.sync="curPage"
      :page-size="10"
      layout="total,prev, pager, next, jumper"
      :total="count">
    </el-pagination>

    <addForm ref="addForm"></addForm>
    <editForm ref="editForm"></editForm>

  </div>


</template>

<script>
import { myReimbursement,deleteMyReimbursement } from "@/request/other_api";
import addForm from "./addForm";
import editForm from "./editForm";

export default {
    components:{
    addForm,
    editForm,
  },
  data() {
    return {
        // 搜索内容
        searchName:'',
        // 页面数据
        tableData: [],
        // count:总数据量
        count:0,
        // 当前页
        curPage:1,
        // 弹出框是否可见
        dialogVisible: false,
        // 等级
        level:'',
        // 是否在搜索状态
        isSearch:false,
        key:Number,
    }
  },
  mounted () {
    this.level = sessionStorage.getItem('level')
  },
  created() {
    //获取初始列表数据
    myReimbursement().then(res => {
      if(res.code == 200){
        this.count = res.count
        this.tableData = res.data
      }
    })
  },
  methods: {
    // 搜索
    search(){
      this.isSearch = true
      myReimbursement({
        keyword:this.searchName,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
      this.curPage = 1
    },
    // 取消搜索
    clearSearch(){
      this.isSearch = false
      myReimbursement({
      }).then(res => {
        if(res.code === 200){
            this.count = res.count
            this.tableData = res.data
        }
      })
      this.curPage = 1
      this.searchName=''
    },
    //点击新增报销单
    add(){
      this.$refs.addForm.dialogVisible = true
      this.$refs.addForm.addMyReimbursement()
    },
    //查看报销单详情
    see(scope){
      let routeData = this.$router.resolve(`/reimbursementDetail/${scope.row.expense_id}`)
      window.open(routeData.href, '_blank')
    },
    //删除某条报销单
    deleteData(data){
      deleteMyReimbursement({
        expense_id:data.expense_id
      }).then(res =>{
        if (res.code == 200) {
          this.getMyReimbursementList()
          this.$message.success('Delete Success!')
        }
      })
    },
    //编辑已保存的报销单
    edit(data){
      this.$refs.editForm.dialogVisible = true
      this.$refs.editForm.editMyReimbursementDetail(data.expense_id)
    },
    /**
     * 分页
     */
    //  点击每页刷新获取数据
    curChange (page) {
      this.curPage = page
      if(this.isSearch) {
        this.getMyReimbursementSearchList()
      } else {
        this.getMyReimbursementList()
      }
    },
    // 上一页
    prevClick () {
    this.curPage < 1 ? this.curPage = 1 : this.curPage--
      if(this.isSearch) {
        this.getMyReimbursementSearchList()
      } else {
        this.getMyReimbursementList()
      }
    },
    // 下一页
    nextClick () {
    this.curPage >= this.count ? this.curPage = this.count : this.curPage++
      if(this.isSearch) {
        this.getMyReimbursementSearchList()
      } else {
        this.getMyReimbursementList()
      }
    },
    //获取搜索列表分页
    getMyReimbursementSearchList(){
      myReimbursement({
        page: this.curPage,
        keyword:this.searchName,
      }).then(res => {
        this.count = res.count
        this.tableData = res.data
      })
    },
    //获取已有报销单列表分页
    getMyReimbursementList(){
      myReimbursement({
        page: this.curPage,
      }).then(res => {
        if(res.code === 200){
          this.count = res.count
          this.tableData = res.data
        }
      })
    },
  },
}
</script>

<style lang="less">
    .search {
      display: flex;
      justify-content: space-between;
      padding-bottom: 20px;
    }
    .permitImg{
      width: 35px;
      height: 35px;
    }
    .dig-form{
      width: 90%;
      height: 100%;
      margin: 0 auto;
    }
    .preImg{
      width:56px;
      border-radius:4px;
      height:40px;
      margin-right:15px
    }
    .title{
      font-family: SourceHanSansSC;
      font-weight: 700;
      font-size: 20px;
      color: rgb(16, 16, 16);
      font-style: normal;
      letter-spacing: 0px;
      line-height: 30px;
      text-decoration: none;
      margin-bottom: 10px;
    }
</style>


